<?php

class User extends BaseEntity{
	
	public static $type = "User";
		
	function __construct($id = 0){
		$this->logicDelete = true;
		parent::__construct($id);
	}			
	
	function setPassword($pass){
		$this->attributes->password = $this->getPassword($pass);
	}
	
	function getPassword($pass){
		return md5($pass);		
	}
	
	function getConfirmationKey(){
		return md5($this->attributes->email);
	}
	
	function validateRequired(){
				
		$exist = new User();
		$exist->getByEmail($this->attributes->email);
		
		if($exist->attributes && $this->attributes->id != $exist->attributes->id){
			logError(langEcho("user:email:duplicate"));
			return false;
		}
		
		
		if($this->attributes->firstName == langEcho("firstname") || $this->attributes->lastName == langEcho("lastname")){
			logError(langEcho("firstNameAndLastNameRequired"));
			return false;
		}
		
					
		return true;
	}
	
	function login($email, $pass, $remember = false){
		if ($pass != ""){
			$where = " email = '" . $email . "' and password = '" . $this->getPassword($pass) . "' and active = 1";
			$this->getByPattern($where);
			return loginUser($this, $remember);
		}
		
		return false;
	}
	
	function getByEmail($email){
		return $this->getByPattern("email = '$email'");		
	}
	
	function canEdit(){	

		if(parent::canEdit()){
			return true;
		}
		
		$user = getUserLoggedIn();
		
		if(isset($user))
		{
			return $user->attributes->id == $this->attributes->id;
		}
		return false;
	}
}
?>